<?php
/**
 * Created by PhpStorm.
 * User: windon
 * Date: 2018/6/3
 * Time: 14:53
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class JpushMobile extends Model
{
    protected $table = 'wy_jpush_mobile';
    public $timestamps = false;
    protected $primaryKey = 'id';

    /*
     * 存储设备标识
     * @$params array
     * */
    public static function saveJpush($params)
    {
        // 推送渠道
        $push_channel = isset($params['push_channel']) ? $params['push_channel'] : 1;

        if(!is_array($params) || !isset($params['mobile_unique_code']))
            return false;

        $model = self::where([ [ 'mobile_unique_code', '=', $params['mobile_unique_code'] ] ])->first();
        if(empty($model) ){
            $model = new JpushMobile();
            $model->mobile_unique_code = $params['mobile_unique_code'];
            $model->option_time = time();
            $model->registration_id = $params['registration_id'];
            $model->push_channel_id = $push_channel;
            $model->create_time = time();
            if($model->save())
                return true;
        }

        if($model->registration_id == $params['registration_id'] && $model->push_channel_id == $push_channel)
            return true;

        $updateArr['registration_id'] = $params['registration_id'];
        $updateArr['push_channel_id'] = $push_channel;
        if(self::where(['id'=>$model->id])->update($updateArr))
            return true;
        return false;
    }

    public static function getRowsArray($where)
    {
        $rows = self::where($where)->first();
        $rows = (empty($rows) ? '': $rows->toArray());
        return $rows;
    }

}